﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Linq;

namespace SearchAssembly
{
    /// <summary>
    /// Search assembly page
    /// </summary>
    public partial class frmSearchAssembly : SearchAssembly.frmMaster
    {
        List<FileInfo> AssembliesList = new List<FileInfo>();
        List<CustomAssembly> allReferences = new List<CustomAssembly>();

        /// <summary>
        /// Constructor
        /// </summary>
        public frmSearchAssembly()
        {
            InitializeComponent();
        }

        private void btnAddAssemblies_Click(object sender, EventArgs e)
        {
            if (browseAssemblies.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {                
                AssembliesList = AssemblyHelper.GetAssemblies(browseAssemblies.SelectedPath).Distinct ().ToList ();

                if (AssembliesList.Count > 0)
                {
                    lstAssemblies.Items.Clear();
                    allReferences.Clear();

                    foreach (FileInfo currentAssembly in AssembliesList)
                    {
                        lstAssemblies.Items.Add(currentAssembly.Name);

                        allReferences.Add (AssemblyHelper.GetReferences (currentAssembly));
                    }
                }
                else
                {
                    MessageBox.Show(this, "No assemblies found", "Nothing found");
                }
            }
        }

        private void txtAssembly2Search_TextChanged(object sender, EventArgs e)
        {
            if (lstAssemblies.Items.Count > 0)
            {
                string assembly2Search = txtAssembly2Search.Text.Trim();
                if (!assembly2Search.Equals(string.Empty))
                {
                    lstSearchResults.Items.Clear();
                    List<string> searchResults = new List<string>();

                    foreach (CustomAssembly currentAssembly in allReferences)
                    {
                        searchResults.AddRange(currentAssembly.References.FindAll(
                            delegate(string reference4Search)
                            {
                                return reference4Search.ToLower().Contains(assembly2Search.ToLower());
                            }
                            ).ToArray());
                    }

                    lstSearchResults.Items.AddRange(searchResults.Distinct().ToArray());
                }
                else
                {
                    lstSearchResults.Items.Clear();
                }
            }
            else
            {
                if (!txtAssembly2Search.Text.Trim().Equals(string.Empty))
                {
                    txtAssembly2Search.Text = string.Empty;
                    MessageBox.Show(this, "add assemblies first to perform search", "No assemblies to search");
                }
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            WizardHelper.LoadPage(this, PageType.Welcome);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (saveList.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                // Populate list
                ApplicationData.SearchResultsDataTable newSearchResults = new ApplicationData.SearchResultsDataTable();

                for (int listItemCounter = 0; listItemCounter < lstSearchResults.Items.Count; listItemCounter++)
                {
                    newSearchResults.AddSearchResultsRow (txtAssembly2Search.Text.Trim (), lstSearchResults.Items[listItemCounter].ToString ());
                }

                if (newSearchResults.Rows.Count > 0)
                {
                    newSearchResults.WriteXml(saveList.FileName);
                    MessageBox.Show(this, "Export complete", "Exporting search results ...");
                }
                else
                {
                    MessageBox.Show(this, "No results found to export", "Saving search results ...");
                }
            }
        }
    }
}
